# !/usr/bin/env python
# -*- encoding: utf-8 -*-
import datetime, os


class C3(object):
    """


    LOGIC	PARAMETER	VALUE	VALUE_NAM	ITEM_ID	SPEC	SPEC_SHOW
    """
    logic = None
    py_code = None
    has_db2_sql_code = False

    # like 'ST_NO'/'IN_MAT_WIDTH,MAT_ACT_WIDTH'
    parameters = None

    # qs_code value, like Q,S,A,B,1,0......
    value = None
    # like 取向, 无取向......
    value_name = None

    item_id = None
    # like QS_CODE, IN_MAT_DIV_CODE
    spec = None
    # like QS_NAME, IN_MAT_DIV
    spec_show = None

    def __init__(self, p_row=None):
        #
        self.has_db2_sql_code = False if (p_row['LOGIC'] is None or len(p_row['LOGIC']) <= 0) else True
        self.logic = p_row['LOGIC'].upper() if self.has_db2_sql_code else None
        self.py_code = None
        #
        self.parameters = p_row['PARAMETER'].split(",")
        #
        self.value = p_row['VALUE']
        self.value_name = p_row['VALUE_NAM']
        #
        self.item_id = p_row['ITEM_ID']
        #
        self.spec = p_row['SPEC']
        self.spec_show = p_row['SPEC_SHOW']

        if self.has_db2_sql_code:
            self.__generate_py_code()

    def get_parameters(self):
        return self.parameters

    def __generate_py_code(self):
        """

        :return:
        """
        self.py_code = self.logic.replace("NVL", "XDB2_2_PY.nvl")
        self.py_code = self.py_code.replace("SUBSTR", "XDB2_2_PY.substr")
        self.py_code = self.py_code.replace(" NOT IN ", " not in ")
        self.py_code = self.py_code.replace(")NOT IN ", ") not in ")
        self.py_code = self.py_code.replace("NOT IN(", " not in (")
        self.py_code = self.py_code.replace(" IN ", " in ")

        self.py_code = self.py_code.replace("=", "==")
        self.py_code = self.py_code.replace("^==", " != ")
        self.py_code = self.py_code.replace("<==", "<=")
        self.py_code = self.py_code.replace(">==", ">=")
        for p in self.parameters:
            self.py_code = self.py_code.replace(p, 'x.%s' % (p))
        return self.py_code
